package io.renren.modules.generator.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import io.renren.common.utils.PageUtils;
import io.renren.common.utils.Query;
import io.renren.constant.DeviceFinal;
import io.renren.modules.generator.dao.DeviceSiteDao;
import io.renren.modules.generator.entity.DeviceSiteEntity;
import io.renren.modules.generator.service.DeviceSiteService;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Objects;


@Service("deviceSiteService")
public class DeviceSiteServiceImpl extends ServiceImpl<DeviceSiteDao, DeviceSiteEntity> implements DeviceSiteService {

    @Autowired
    private DeviceSiteDao deviceSiteDao;

    @Override
    public PageUtils queryPage(Map<String, Object> params,DeviceSiteEntity deviceSite) {
        QueryWrapper<DeviceSiteEntity> wrapper = new QueryWrapper<>();
        if(Objects.nonNull(deviceSite.getDeviceCode())){
            wrapper.like("device_code",deviceSite.getDeviceCode());
        }
        if(Objects.nonNull(deviceSite.getSiteName())){
            wrapper.like("site_name",deviceSite.getSiteName());
        }
        wrapper.orderByAsc("id");
        IPage<DeviceSiteEntity> page = this.page(
                new Query<DeviceSiteEntity>().getPage(params),
                wrapper
        );

        return new PageUtils(page);
    }

    @Override
    public Map<String, List<DeviceSiteEntity>> selecctAllList(String type,String exclude) {
        Map<String, List<DeviceSiteEntity>> listMap = new HashMap<>();
        List<DeviceSiteEntity> deviceSiteEntityList;
        if (StringUtils.isNotBlank(type)) {
            QueryWrapper<DeviceSiteEntity> queryWrapper = new QueryWrapper<>();
            queryWrapper.lambda().eq(DeviceSiteEntity::getDeviceType,type);
            deviceSiteEntityList = deviceSiteDao.selectList(queryWrapper);
            listMap.put(type,deviceSiteEntityList);
        } else {
            for(Map.Entry<String, String> entry : DeviceFinal.devictTypeMap.entrySet()){
                if (Boolean.valueOf(exclude) && StringUtils.equals(entry.getKey(),DeviceFinal.pm25_type)) {
                    continue;
                }
                String mapKey = entry.getKey();
                QueryWrapper<DeviceSiteEntity> queryWrapper = new QueryWrapper<>();
                queryWrapper.lambda().eq(DeviceSiteEntity::getDeviceType,mapKey);
                deviceSiteEntityList = deviceSiteDao.selectList(queryWrapper);
                listMap.put(mapKey,deviceSiteEntityList);
            }
        }
        return listMap;
    }

}